package com.gitee.wsl.struct.table.mini.dsl

import com.gitee.wsl.struct.table.mini.TableSection

@PicnicDsl
interface TableSectionDsl {
    fun row(vararg cells: Any?) {
        row {
            cells.forEach { cell(it) }
        }
    }

    fun row(content: RowDsl.() -> Unit)

    fun cellStyle(content: CellStyleDsl.() -> Unit)
}

internal class TableSectionDslImpl : TableSectionDsl {
    private val builder = TableSection.Builder()
    private val cellStyleImpl = CellStyleDslImpl()

    override fun row(content: RowDsl.() -> Unit) {
        builder.addRow(RowDslImpl().apply(content).create())
    }

    override fun cellStyle(content: CellStyleDsl.() -> Unit) {
        cellStyleImpl.apply(content)
    }

    fun createOrNull() = if (builder.rows.isEmpty()) null else create()
    fun create() = builder.setCellStyle(cellStyleImpl.createOrNull()).build()
}